﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;

using P2PLib.Client.DataModel;

namespace P2PLib.Client.Utils
{
    /// <summary>
    /// 该类包含与下载任务有关的工具类;
    /// </summary>
    public class DownloadTaskUtils
    {
        /// <summary>
        /// Save the download task list to path
        /// </summary>
        /// <param name="tasks"></param>
        /// <param name="path"></param>
        public static void SaveDownloadTasks(DownloadTaskList list,string path)
        {
            FileInfo file = new FileInfo(path);
            if (file.Exists)
                file.Delete();

            XmlSerializer xs = new XmlSerializer(typeof(DownloadTaskList));
            FileStream fs = new FileStream(path,FileMode.CreateNew);

            xs.Serialize(fs, list);

        }

        /// <summary>
        /// 从指定的路径加载下载任务列表
        /// </summary>
        /// <param name="path"></param>
        public static DownloadTaskList LoadDownloadTasks(string path)
        {
            XmlSerializer xs = new XmlSerializer(typeof(DownloadTaskList));

            FileStream fs = new FileStream(path, FileMode.Open);

            DownloadTaskList list = (DownloadTaskList)xs.Deserialize(fs);
            return list;
        }
    }
}
